package com.tanhua.sso.controller;

import com.tanhua.common.pojo.User;
import com.tanhua.sso.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/*
 * 用户控制层
 * */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    //注入service
    @Autowired
    private UserService userService;

    /*
     * ResponseEntity:
     *   响应前端数据及相应的状态码
     *
     * 发送验证码的操作
     * */
    @PostMapping("login")
    public ResponseEntity login(@RequestBody Map<String, String> map) { //String phone {phone:"135xxxxxxx"} -->对象 map
        //获取手机号
        String phone = map.get("phone");
        //调用业务层发送手机号
        userService.sendMobilePhone(phone);
        return ResponseEntity.ok(null);
    }

    /*
     * 手机号和验证码登录
     *
     * */
    @PostMapping("loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> map) {
        //获取手机号和验证码
        String phone = map.get("phone");
        String code = map.get("verificationCode");
        //map:["token:"","isNew":boolean ]
        Map<String, Object> result = userService.checkMobileAndCode(phone, code);
        return ResponseEntity.ok(result);
    }

    /*
     * 对外提供解析token的功能
     * */
    @GetMapping("{token}")
    public User queryToken(@PathVariable("token") String token) {
        return userService.queryToken(token);
    }

}
